<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
            "http://www.w3.org/TR/REC-html40/loose.dtd">
<HTML>
<HEAD>



<META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<META name="GENERATOR" content="hevea 1.08">
<LINK rel="stylesheet" type="text/css" href="tutorial.css">
<TITLE>
Defining Your Own Predicates
</TITLE>
</HEAD>
<BODY >
<A HREF="tutorial015.html"><IMG SRC ="previous_motif.gif" ALT="Previous"></A>
<A HREF="tutorial012.html"><IMG SRC ="contents_motif.gif" ALT="Up"></A>
<A HREF="tutorial017.html"><IMG SRC ="next_motif.gif" ALT="Next"></A>
<HR>

<H2 CLASS="section"><A NAME="htoc33">3.4</A>&nbsp;&nbsp;Defining Your Own Predicates</H2><UL>
<LI><A HREF="tutorial016.html#toc19">Comments</A>
<LI><A HREF="tutorial016.html#toc20">Clauses and Predicates</A>
</UL>

<A NAME="toc19"></A>
<H3 CLASS="subsection"><A NAME="htoc34">3.4.1</A>&nbsp;&nbsp;Comments</H3>
<A NAME="@default61"></A>
 Since we will annotate some of our programs, we first introduce
 the syntax for comments. There are two types:
 <DL CLASS="description" COMPACT=compact><DT CLASS="dt-description">
 <B>Block comment</B><DD CLASS="dd-description"> The comment is enclosed between the delimiters <TT>/*</TT> and <TT>*/</TT>.
 Such comments can span multiple lines, and may be conveniently used
 to comment out unused code.
 <DT CLASS="dt-description"><B>Line comment</B><DD CLASS="dd-description"> Anything following and including '<TT>%</TT>' in a line is taken as a
 comment (unless 
 the '<TT>%</TT>' character is part of a quoted atom or string).
 </DL>
<A NAME="toc20"></A>
<H3 CLASS="subsection"><A NAME="htoc35">3.4.2</A>&nbsp;&nbsp;Clauses and Predicates</H3>
<A NAME="syntax"></A>
<A NAME="@default62"></A>
<A NAME="@default63"></A> <A NAME="@default64"></A>
 Prolog programs are built from valid Prolog data-structures.
 A program is a collection of <I>predicates</I>, and a
 predicate is a collection of <I>clauses</I>.<BR>
<BR>
The idea of a clause is to define that something is true.
 The simplest form of a clause is the <EM>fact</EM>.
 For example, the following two are facts:

	<TABLE CELLPADDING=10>
<TR><TD BGCOLOR="#CCCCFF">
	<BLOCKQUOTE CLASS="quote"><PRE>
capital(london, england).
brother(fred, jane).
</PRE></BLOCKQUOTE></TD>
</TR></TABLE>
 Syntactically, a fact is just a structure (or an atom)
 terminated by a full stop.<BR>
<BR>
Generally, a clause has the form
 <BLOCKQUOTE CLASS="quote">
 Head :- Body.
 </BLOCKQUOTE>
 where <EM>Head</EM> is a structure (or atom) and <EM>Body</EM> is a <EM>Goal</EM>,
 possibly with conjunctions and disjunctions like in the queries discussed above.
 The following is a clause

	<TABLE CELLPADDING=10>
<TR><TD BGCOLOR="#CCCCFF">
	<BLOCKQUOTE CLASS="quote"><PRE>
uncle(X,Z) :- brother(X,Y), parent(Y,Z).
</PRE></BLOCKQUOTE></TD>
</TR></TABLE>
 Logically, this can be read as a reverse implication
 <BLOCKQUOTE CLASS="quote">
 <I>uncle</I>(<I>X</I>,<I>Z</I>) &larr;&mdash; <I>brother</I>(<I>X</I>,<I>Y</I>) &and; <I>parent</I>(<I>Y</I>,<I>Z</I>)
 </BLOCKQUOTE>
 or, more precisely
 <BLOCKQUOTE CLASS="quote">
 &forall; <I>X</I> &forall; <I>Z</I>: <I>uncle</I>(<I>X</I>,<I>Z</I>) &larr;&mdash; &exist; <I>Y</I>: <I>brother</I>(<I>X</I>,<I>Y</I>) &and; <I>parent</I>(<I>Y</I>,<I>Z</I>)
 </BLOCKQUOTE>
 stating that uncle(X,Z) is true if brother(X,Y) and parent(Y,Z) are true.
 Note that a fact is equivalent to a clause where the body is <TT>true</TT>:

	<TABLE CELLPADDING=10>
<TR><TD BGCOLOR="#CCCCFF">
	<BLOCKQUOTE CLASS="quote"><PRE>
brother(fred, jane) :- true.
</PRE></BLOCKQUOTE></TD>
</TR></TABLE><BR>
One or multiple clauses with the same head functor (same name and number
 of arguments) together form the <EM>definition</EM>
 of a predicate. Logically, multiple clauses are read as a disjunction,
 i.e. they define alternative ways in which the predicate can be true.
 The simplest case is a collection of alternative facts:

	<TABLE CELLPADDING=10>
<TR><TD BGCOLOR="#CCCCFF">
	<BLOCKQUOTE CLASS="quote"><PRE>
parent(abe, homer).
parent(abe, herbert).
parent(homer, bart).
parent(marge, bart).
</PRE></BLOCKQUOTE></TD>
</TR></TABLE>
The following defines the ancestor/2 predicate by giving two alternative
clauses (rules):

	<TABLE CELLPADDING=10>
<TR><TD BGCOLOR="#CCCCFF">
	<BLOCKQUOTE CLASS="quote"><PRE>
ancestor(X,Y) :- parent(X,Y).
ancestor(X,Y) :- parent(Z,Y), ancestor(X,Z).
</PRE></BLOCKQUOTE></TD>
</TR></TABLE>
 Remember that a clause can be read logically, with the <TT>:-</TT>
 taking the meaning of implication, and the comma
 separating goals read as a conjunction. The logical
 reading for several clauses of the same predicate is disjunction
 between the clauses. So the first
 ancestor rule above states that if X is a parent of Y, then this
 implies that X is an ancestor of Y. The second rule, which specifies
 another way X can be an ancestor of Y states that if some other
 person, Z, is the parent of Y, <I>and</I> X is an ancestor of Z,
 then this implies that X is also an ancestor of Y.<BR>
<BR>

<TABLE CELLPADDING=10>
<TR><TD BGCOLOR="#CCFFCC">
<DL CLASS="description" COMPACT=compact><DT CLASS="dt-description">
<B>&otimes;</B><DD CLASS="dd-description"> 
<A NAME="@default65"></A>
It is also important to remember that the scope of a variable
 name only extends over the clause in which it is in, so any
 variables with the same name in the same clause refer to the
 same variable, but variables which occur in different clauses
 are different even if they have been written with the same name.
 
</DL>
</TD>
</TR></TABLE>
<BR>
<HR>
<A HREF="tutorial015.html"><IMG SRC ="previous_motif.gif" ALT="Previous"></A>
<A HREF="tutorial012.html"><IMG SRC ="contents_motif.gif" ALT="Up"></A>
<A HREF="tutorial017.html"><IMG SRC ="next_motif.gif" ALT="Next"></A>
</BODY>
</HTML>
